package cn.jiyun.service.impl;

import cn.jiyun.entity.XmSettled;
import cn.jiyun.mapper.XmSettledMapper;
import cn.jiyun.service.IXmSettledService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-25
 */
@Service
public class XmSettledServiceImpl extends ServiceImpl<XmSettledMapper, XmSettled> implements IXmSettledService {


    @Override
    public Page<XmSettled> findAll(Integer current, Integer pageSize, XmSettled xmSettled) {
        Page<XmSettled> xmSettledPage = new Page<>(current, pageSize);
        QueryWrapper<XmSettled> xmSettledQueryWrapper = new QueryWrapper<>();
        if (xmSettled.getId() != null) {
            xmSettledQueryWrapper.eq("id", xmSettled.getId());
        }
        if (xmSettled.getName() != null && !"".equals(xmSettled.getName())) {
            xmSettledQueryWrapper.like("name", xmSettled.getName());
        }
        if (xmSettled.getTel() != null && !"".equals(xmSettled.getTel())) {
            xmSettledQueryWrapper.like("tel", xmSettled.getTel());
        }
        if (xmSettled.getMain() != null && !"".equals(xmSettled.getMain())) {
            xmSettledQueryWrapper.like("main", xmSettled.getMain());
        }
        if (xmSettled.getAddress() != null && !"".equals(xmSettled.getAddress())) {
            xmSettledQueryWrapper.like("address", xmSettled.getAddress());
        }
        if(xmSettled.getShippingMethod()!=null&&!"".equals(xmSettled.getShippingMethod())){
            xmSettledQueryWrapper.eq("shipping_method",xmSettled.getShippingMethod());
        }
        if(xmSettled.getStart()!=null){
            xmSettledQueryWrapper.ge("point",xmSettled.getStart());
        }
        if(xmSettled.getEnd()!=null){
            xmSettledQueryWrapper.le("point",xmSettled.getEnd());
        }
        if ( xmSettled.getZtime()!= null) {
            LocalDateTime now = LocalDateTime.now();
            int mo = 90;
            if ( xmSettled.getZtime() == 1) {
                 xmSettledQueryWrapper.apply("ABS(DATEDIFF({0}, opening_time)) <= {1}", now, mo);
            }

            if( xmSettled.getZtime()==2){
                 xmSettledQueryWrapper.apply("YEAR(opening_time)=YEAR(CURRENT_DATE)");
            }

            if( xmSettled.getZtime()==3){
                 xmSettledQueryWrapper.apply("DATE_FORMAT(opening_time,'%Y')=2022");
            }

            if( xmSettled.getZtime()==4){
                 xmSettledQueryWrapper.apply("DATE_FORMAT(opening_time,'%Y')=2021");
            }
            if( xmSettled.getZtime()==5){
                 xmSettledQueryWrapper.apply("DATE_FORMAT(opening_time,'%Y')=2020");
            }
            if( xmSettled.getZtime()==6){
                 xmSettledQueryWrapper.apply("DATE_FORMAT(opening_time,'%Y')<2020");
            }
        }
        if(xmSettled.getZname()!=null && !"".equals(xmSettled.getZname())){
            xmSettledQueryWrapper.nested(wp -> wp.like("responsible_person", xmSettled.getZname()).or()).nested(wp -> wp.like("juridical_person",xmSettled.getZname() ));
        }

        if(xmSettled.getReviewTime()!=null && !xmSettled.getReviewTime().equals("")){
            xmSettledQueryWrapper.like("review_time",xmSettled.getReviewTime());
        }

        if(xmSettled.getAuditStatus()!=null){
            xmSettledQueryWrapper.eq("audit_status",xmSettled.getAuditStatus());
        }
        if(xmSettled.getReviewers()!=null&&!"".equals(xmSettled.getReviewers())){
            xmSettledQueryWrapper.like("reviewers",xmSettled.getReviewers());
        }
        if(xmSettled.getActiveTab().equals("healthRecords2")){
            xmSettledQueryWrapper.eq("audit_status",8);
        }
        return baseMapper.selectPage(xmSettledPage, xmSettledQueryWrapper);
    }


    @Autowired
    private XmSettledMapper xmSettledMapper;
    @Override
    public Integer findAllSettled() {
        return xmSettledMapper.findAllSettled();
    }

    @Override
    public Integer findAllSettled2() {
        return xmSettledMapper.findAllSettled2();
    }
}
